// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        bgCloseBtn: cc.Node,
        closeBtn: cc.Button,
        okBtn: cc.Button,

        textName: cc.Label,
        textContent: cc.Label,

        tcShow: cc.Sprite,

        currentDialogAction: cc.Action,
        currentTerminalAction: cc.Action,
        isShow: Boolean,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.node.opacity = 0;
        this.node.active = false;
        this.isShow = false;
        // 背景关闭按钮
        this.bgCloseBtn.on(cc.Node.EventType.TOUCH_END, function(evt){
            this.hide();
        }, this);
        //关闭按钮
        this.closeBtn.node.on(cc.Node.EventType.TOUCH_END, function(evt){
            this.hide();
        }, this);
        //确认按钮
        this.okBtn.node.on(cc.Node.EventType.TOUCH_END, function(evt)
        {
            //隐藏界面但不回退终端卡
            this.hide(false);
            //执行回调
            if(this.callback != null)
                this.callback(this.selectName, this.tcOrigin);
        }, this);
        //获取目标点世界坐标
        this.targetPos = this.tcShow.node.position;
    },

    // 显示终端卡信息
    show: function(type, tc, callback, showTime = 0.2) {
        if(this.isShow)
            return;
        //初始化
        this.isShow = true;
        this.terminal = this.tcShow;
        this.callback = callback;
        let worldPos = tc.parent.convertToWorldSpaceAR(tc.position);
        this.originTcBackPos = tc.position;
        this.originTerminalPos = this.node.convertToNodeSpaceAR(worldPos);//获得经过转化的终端卡初始坐标
        this.tcOrigin = tc;
        tc.active = false;//隐藏原本的终端卡
        this.tcDialogPos = tc.parent.convertToNodeSpaceAR(this.node.convertToWorldSpaceAR(this.targetPos));
        tc.position = this.tcDialogPos;//原本的终端卡移动到目标位置
        this.terminal.node.active = true;//显示展示终端卡
        this.terminal.spriteFrame = tc.getComponent(cc.Sprite).spriteFrame;//把原本的终端卡贴图和位置赋值到展示终端卡上
        this.terminal.node.position = this.originTerminalPos;
        //渐显效果
        if(this.currentDialogAction != null && !this.currentDialogAction.isDone())
        {
            this.terminal.node.stopAction(this.currentDialogAction);
        }
        this.node.opacity = 0;
        this.node.active = true;
        this.currentDialogAction = cc.sequence(cc.fadeIn(showTime), cc.callFunc(() => {
            this.node.opacity = 255;
        }));
        this.node.runAction(this.currentDialogAction);
        //让终端卡移动到指定位置
        let localPos = this.targetPos;
        if(this.currentTerminalAction != null && !this.currentTerminalAction.isDone())
        {
            this.terminal.node.stopAction(this.currentTerminalAction);
        }
        this.currentTerminalAction = cc.sequence(cc.spawn(cc.moveTo(showTime, localPos), cc.scaleTo(showTime, 1.2)), cc.callFunc(() => {
            this.terminal.node.position = localPos;
            this.terminal.node.scale = 1.2;
        }));
        this.terminal.node.runAction(this.currentTerminalAction);
        //刷新终端卡文本
        this.refreshTerminalText(type);
    },

    //隐藏信息
    hide: function(terminalNeedReturn = true, hideTime = 0.2)
    {
        if(!this.isShow)
            return;
        this.isShow = false;
        this.terminal.node.active = false;//隐藏显示用终端卡
        this.tcOrigin.active = true;
        //渐隐效果
        if(this.currentDialogAction != null && !this.currentDialogAction.isDone())
        {
            this.node.stopAction(this.currentDialogAction);
        }
        this.node.opacity = 255;
        this.currentDialogAction = cc.sequence(cc.fadeOut(hideTime), cc.callFunc(() => {
            this.node.opacity = 0;
            this.node.active = false;
        }));
        this.node.runAction(this.currentDialogAction);
        //让终端卡移动到原本位置
        if(this.currentTerminalAction != null && !this.currentTerminalAction.isDone())
        {
            this.tcOrigin.stopAction(this.currentTerminalAction);
        }
        if(terminalNeedReturn)
        {
            this.currentTerminalAction = cc.sequence(cc.spawn(cc.moveTo(hideTime, this.originTcBackPos), cc.scaleTo(hideTime, 1)), cc.callFunc(() => {
                this.tcOrigin.position = this.originTcBackPos;
                this.tcOrigin.scale = 1;
            }));
            this.tcOrigin.runAction(this.currentTerminalAction);
        }
    },

    refreshTerminalText : function(selectName)
    {
        this.selectName = selectName;
        switch(selectName)
        {
            case 'lineboost':
                this.textName.string = '超速回线\nLINE BOOST';
                this.textContent.string = 
'\n装备终端卡\n\
①可装备在棋盘上自己的棋子上或从棋子上取下,装备或取下该终端卡需要消耗一回合。一枚棋子只能装备一张装备卡。\n\
②装备超速回线的棋子从棋盘上离开时装备的超速回线卡视为被取下，可再次进行装备。\n\
③装备超速回线的棋子一次移动的最大距离变成2格。';
            break;
            case 'firewall':
                this.textName.string = '防火墙\nFIRE WALL';
                this.textContent.string = 
'\n终端卡\n\
①可在没有棋子和防火墙的棋盘格上放置或从棋盘上取回，放置或取回需要消耗一回合。\n\
②对方棋子无法移动进入我方防火墙所在棋盘格。';
            break;
            case 'viruscheck':
                this.textName.string = '病毒探查\nVIRUS CHECK';
                this.textContent.string = 
'\n终端卡\n\
①一场游戏中只能使用一次,使用该终端卡需要消耗一回合。\n\
②指定棋盘上一枚对方未亮明身份的棋子，使其亮明身份。';
            break;
            case 'notfound':
                this.textName.string = '404 NOT FOUND';
                this.textContent.string = 
'\n终端卡\n\
①一场游戏中只能使用一次,使用该终端卡需要消耗一回合。\n\
②指定棋盘上两枚我方棋子，让它们交换或不交换位置，对方不知道是否交换。\n\
③交换的棋子如果已亮明身份，则翻回背面。所带有的装备留在原位置。';
            break;
            case 'randomverification':
                this.textName.string = '访问验证\nRANDOM VERIFICATION';
                this.textContent.string = 
'\n终端卡\n\
①一场游戏中只能使用一次,使用该终端卡需要消耗一回合。\n\
②在自己一枚棋子所在处设置一个访问验证格，对方不知道位置。\n\
③棋子进入验证格时消耗验证格，该棋子亮明身份，解除装备并结束回合。\n\
④移动路径的起点终点连线经过验证格的棋子，其移动终点会被改为验证格所在位置。';
            break;
            case 'zerodayattack':
                this.textName.string = '零日攻击\nZERO-DAY ATTACK';
                this.textContent.string = 
'\n终端卡\n\
①一场游戏中只能使用一次,使用该终端卡需要消耗一回合。\n\
②选择自己一枚棋盘上的棋子，向前移动最多3格，前进路径上有敌方棋子时视为攻击（吃掉）并终止移动。\n\
③因该终端卡造成的移动不受防火墙及最大移动距离影响。';
            break;
            case 'sandbox':
                this.textName.string = '沙盒检测\nSAND BOX';
                this.textContent.string = 
'\n装备终端卡\n\
①一场游戏中只能使用一次,可装备在棋盘内的棋子上,装备该终端卡需要消耗一回合。一枚棋子只能装备一张装备卡。\n\
②装备棋子必须装备有SAND BOX以外的终端卡，将所选棋子上的装备取下并作为代替装备该终端卡。\n\
③装备棋子无法成为拥有使棋子翻面效果的终端卡的选择对象。\n\
④装备该终端卡的棋子在未亮明身份时一次移动的最大距离变为2格，亮明身份若为link变为1，为virus变为0（不可移动）。\n\
⑤装备该终端卡的棋子在进攻对方棋子后亮明身份。';
            break;
        }
    }
});
